package com.jieao.backend.controller.content;

import com.jieao.common.annotation.Log;
import com.jieao.common.core.controller.BaseController;
import com.jieao.common.core.domain.AjaxResult;
import com.jieao.common.core.domain.Ztree;
import com.jieao.common.core.page.TableDataInfo;
import com.jieao.common.enums.BusinessType;
import com.jieao.content.dto.DiaryDto;
import com.jieao.content.service.IDiaryService;
import com.jieao.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Description 日记管理
 * @Author weifanl
 * @Date2020/5/21 15:31
 **/
@Controller
@RequestMapping("/content/diary")
public class DiaryController extends BaseController {
    private String prefix = "backend/content/diary";

    @Autowired
    private IDiaryService diaryService;

    @RequiresPermissions("content:diary:view")
    @GetMapping()
    public String diary() {
        return prefix + "/diary";
    }

    @RequiresPermissions("content:diary:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DiaryDto diary) {
        startPage();
        List<DiaryDto> list = diaryService.selectDiaryList(diary);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存配置
     */
    @Log(title = "日记管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("content:diary:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated DiaryDto diary) {
        diary.setCreateBy(ShiroUtils.getLoginName());
        diary.setCreateTime(new Date());
        return toAjax(diaryService.insertDiary(diary));
    }

    /**
     * 修改
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        DiaryDto diary = diaryService.selectDiaryById(id);
        mmap.put("diary", diary);
        return prefix + "/edit";
    }

    /**
     * 保存
     */
    @Log(title = "日记管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("content:diary:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated DiaryDto catalog) {
        return toAjax(diaryService.updateDiary(catalog));
    }

    /**
     * 日记状态修改
     */
    @Log(title = "日记管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("content:diary:edit")
    @PostMapping("/change")
    @ResponseBody
    public AjaxResult change(DiaryDto catalog) {
        return toAjax(diaryService.change(catalog));
    }

    /**
     * 日记删除
     * @return com.jieao.common.core.domain.AjaxResult
     * @Author weifanl
     * @Date 18:31 2020/5/27
     * @Param [ids]
     **/
    @RequiresPermissions("content:diary:remove")
    @Log(title = "日记管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(diaryService.deleteDiaryByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    //日记选择界面
    /**
     * 选择日记树
     */
    @GetMapping("/selectDiaryTree/{id}")
    public String selectDiaryTree(@PathVariable("id") String id, ModelMap mMap) {
        if("null".equals(id)){
            mMap.put("diary", new DiaryDto());
        }else{
            mMap.put("diary", diaryService.selectDiaryById(Long.valueOf(id)));
        }
        return prefix + "/diaryTree";
    }

    /**
     * 加载目录列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData(DiaryDto diaryDto) {
        List<Ztree> ztrees = diaryService.selectDiaryTree(diaryDto);
        return ztrees;
    }
}
